package com.twu.biblioteca;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;

public class BibliotecaApp {

    private final PrintStream printStream;
    private final BufferedReader bufferedReader;

    public BibliotecaApp(PrintStream printStream, BufferedReader bufferedReader) {
        this.printStream = printStream;
        this.bufferedReader = bufferedReader;
    }

    public static void main(String[] args) {
        new BibliotecaApp(System.out, new BufferedReader(new InputStreamReader(System.in))).run();
    }

    private static List<ItemStatus> initialiseItemStatus() {
        List<Item> items = new ArrayList<>();
        items.add(new Book("Harry Potter", 2000));
        items.add(new Book("The Hobbit", 1880));
        items.add(new Book("The Tempest", 2005));
        items.add(new Book("Clean Code", 2008));
        items.add(new Movie("LOTR", 2003, "P. Jackson", 10));
        items.add(new Movie("Lego", 2013, "C. Millers", 8));
        List<ItemStatus> statuses = new ArrayList<>();
        statuses.add(new ItemStatus(items.get(0), 1));
        statuses.add(new ItemStatus(items.get(1), 4));
        statuses.add(new ItemStatus(items.get(2), 1));
        statuses.add(new ItemStatus(items.get(3), 0));
        statuses.add(new ItemStatus(items.get(4), 2));
        statuses.add(new ItemStatus(items.get(5), 2));
        return statuses;
    }

    public List<User> initialiseUsers() {
        List<User> users = new ArrayList<>();

        users.add(new User.UserBuilder()
                .setName("Joe Bloggs")
                .setEmail("joe.bloggs@gmail.com")
                .setPhoneNumber("0161-123-4567")
                .setLibraryNumber("111-222-333")
                .setPassword("Pa55w0rd")
                .build());

        return users;
    }

    public void run() {
        Library library = new Library(initialiseItemStatus(), initialiseUsers());
        UserInterfaceState state = UserInterfaceState.WELCOME;
        state = state.execute(library, printStream, bufferedReader);
        while (true) {
            state = state.execute(library, printStream, bufferedReader);
        }
    }
}
